package com.sumover.controller;

import com.sumover.POJO.*;
import com.sumover.Service.CommentService;
import com.sumover.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

@RequestMapping("/comment")
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;


    /**
     * @param game
     * @return
     */
    @RequestMapping("/getGameComments")
    public ResultVo getGameComments(@RequestBody Game game) {
        List<Level> gameLevels = commentService.getGameLevels(game);
        return ResultVo.success("all game levels returned", gameLevels);
    }

    /**
     * @param article
     * @return
     */
    @RequestMapping("/getArticleComments")
    public ResultVo getArticleComments(@RequestBody Article article) {
        List<Level> articleLevels = commentService.getArticleLevels(article);
        return ResultVo.success("all article levels returned", articleLevels);
    }

    /**
     * 在数据库中插入一个游戏评论
     *
     * @param game
     * @param commentText
     * @param session
     * @return
     */
    @RequestMapping("/addNewGameComment")
    public ResultVo addNewGameComments(@RequestBody Game game, @RequestBody String commentText, HttpSession session) {
        commentService.addNewGameComments(game, commentText, (User) session.getAttribute("loginUser"));
        return ResultVo.success("add new game comment", null);
    }

    /**
     * 在数据库中插入一个文章的评论
     *
     * @param article
     * @param commentText
     * @param session
     * @return
     */
    @RequestMapping("/addNewArticleComment")
    public ResultVo addNewArticleComment(@RequestBody Article article, @RequestBody String commentText, HttpSession session) {
        commentService.addNewArticleComment(article, commentText, (User) session.getAttribute("loginUser"));
        return ResultVo.success("add new article comment", null);
    }

    /**
     * 在文章中插入一个回复.
     *
     * @param article
     * @param replyToLevel
     * @param replyText
     * @param session
     * @return
     */
    @RequestMapping("/addNewReplyInArticle")
    public ResultVo addNewReplyInArticle(@RequestBody Article article, @RequestBody Integer replyToLevel, @RequestBody String replyText, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) return ResultVo.error("user not login!");
        commentService.addNewReplyInArticle(article, replyToLevel, replyText, loginUser);
        return ResultVo.success("new reply add success!", null);
    }

    /**
     * 在游戏中插入一条回复
     *
     * @param game
     * @param replyToLevel
     * @param replyText
     * @param session
     * @return
     */
    @RequestMapping("/addNewReplyInGame")
    public ResultVo addNewReplyInGame(@RequestBody Game game, @RequestBody Integer replyToLevel, @RequestBody String replyText, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) return ResultVo.error("user not login!");
        commentService.addNewReplyInGame(game, replyToLevel, replyText, loginUser);
        return ResultVo.success("new reply add success!", null);
    }


}
